package com.irdstudio.apicenter.frame.core.jdbc.container;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

/**
 * 数据集合,由数据行组成 封装对List的调用
 * 
 * @author zs.ligm
 * @version 1.0
 * @date 2015-07-20
 */
public class DataList implements List<DataRow> {

	/* 数据行集合 */
	private List<DataRow> rowList;

	/**
	 * 直接构造DataList对象
	 */
	public DataList() {
		rowList = new ArrayList<DataRow>();
	}

	public DataList(List<Map<String, Object>> dataList) {
		rowList = new ArrayList<DataRow>();
		for (int i = 0; i < dataList.size(); i++) {
			DataRow row = new DataRow(dataList.get(i));
			rowList.add(row);
		}
	}
	
	@Override
	public String toString() {
		return toString(0);
	}

	/**
	 * 格式化成为xml字符串
	 * 
	 * @param tabCount
	 * @return
	 */
	public String toString(int tabCount) {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < tabCount; ++i)
			buf.append("\t");
		buf.append("<list id=\"");
		// buf.append(getName());
		buf.append("\">\n");

		if (this.rowList.size() > 0) {
			for (int i = 0; i < this.rowList.size(); ++i) {
				DataRow row = this.rowList.get(i);
				buf.append(row.toString(tabCount + 1));
				buf.append("\n");
			}
		}

		for (int i = 0; i < tabCount; ++i)
			buf.append("\t");
		buf.append("</list>");

		return buf.toString();
	}

	@Override
	public boolean add(DataRow row) {
		return rowList.add(row);
	}

	@Override
	public void add(int index, DataRow element) {
		rowList.add(index, element);
	}

	@Override
	public boolean addAll(Collection<? extends DataRow> c) {
		return rowList.addAll(c);
	}

	@Override
	public boolean addAll(int index, Collection<? extends DataRow> c) {
		return rowList.addAll(index, c);
	}

	@Override
	public void clear() {
		rowList.clear();
	}

	@Override
	public boolean contains(Object o) {
		return rowList.contains(o);
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		return rowList.containsAll(c);
	}

	@Override
	public DataRow get(int index) {
		return rowList.get(index);
	}

	@Override
	public int indexOf(Object o) {
		return rowList.indexOf(o);
	}

	@Override
	public boolean isEmpty() {
		return rowList.isEmpty();
	}

	@Override
	public Iterator<DataRow> iterator() {
		return rowList.iterator();
	}

	@Override
	public int lastIndexOf(Object o) {
		return rowList.lastIndexOf(o);
	}

	@Override
	public ListIterator<DataRow> listIterator() {
		return rowList.listIterator();
	}

	@Override
	public ListIterator<DataRow> listIterator(int index) {
		return rowList.listIterator(index);
	}

	@Override
	public boolean remove(Object o) {
		return rowList.remove(o);
	}

	@Override
	public DataRow remove(int index) {
		return rowList.remove(index);
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		return rowList.removeAll(c);
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		return rowList.retainAll(c);
	}

	@Override
	public DataRow set(int index, DataRow element) {
		return rowList.set(index, element);
	}

	@Override
	public int size() {
		return rowList.size();
	}

	@Override
	public List<DataRow> subList(int fromIndex, int toIndex) {
		return rowList.subList(fromIndex, toIndex);
	}

	@Override
	public Object[] toArray() {
		return rowList.toArray();
	}

	@Override
	public <T> T[] toArray(T[] a) {
		return rowList.toArray(a);
	}
}
